$(function() {
	"use strict";
	
	var initTenantTable = function () {

        var table = $('#tenant_table');

        table.DataTable({
        	"searching": false,
            "language": {
                "emptyTable": "暂无数据",
                "loadingRecords": "请稍候 - 数据加载中...",
                "info": "显示 _START_ 到 _END_ 共 _TOTAL_ 条记录",
                "infoEmpty": "",
                "infoFiltered": "",
                "lengthMenu": "_MENU_",
                "zeroRecords": "暂无数据",
                "paginate": {
                    "previous":"上一页",
                    "next": "下一页",
                    "last": "最后一页",
                    "first": "第一页"
                }
            },
            "ordering": false,
            "lengthChange":false,
            "pageLength": 20,
            "pagingType": "full_numbers",
            "serverSide": true,
            "ajax": {
                url: context+"/app/tenant/list",
                type: 'POST',
                data:function(d){
                	var queryForm = $(table.data("queryForm"));
                	var x=queryForm.serializeArray();
            	    $.each(x, function(i, field){
            		 d[field.name] = field.value;
            	    });
                },
                dataSrc: 'tenantList'
            },
            "columns": [
                { "data": "code" },
                { "data": "name" },
                { "data": "company" },
                { "data": "charger" },
                { "data": "tel" },
                { "data": "email" },
                { "data": "loginName" },
                { "data": "id",
                  "width":200,
                  "render": function ( data, type, full, meta ) {
                    return '<a href="javascript:;" class="btn btn-default btn-sm edit" data-edit-id='+data+'><i class="fa fa-edit"></i> 编辑商户 </a>&nbsp;'+
                    	   '<a href="javascript:;" class="btn btn-danger btn-sm remove" data-remove-id="'+data+'"><i class="fa fa-remove"></i> 删除商户 </a>&nbsp;'
                  }
                }
            ]
        });
    }
	
	var initAddTenant = function(){
		$(".addTenant").on('click',function(){
			var dialog = $("#tenantAddDialog");
			var form = $("#tenantForm",dialog);
			form[0].reset();
			dialog.modal();
		});
		
		$(".ok","#tenantAddDialog").on('click',function(){
			var dialog = $("#tenantAddDialog");
			var form = $("#tenantForm",dialog);
			
			App.blockUI({
                target: '#tenantAddDialogContent',
                boxed: true,
                message: '数据处理中...'
            });
			
			$.ajax({
	            url:context+'/app/tenant/save',
	            type:'POST',
	            data:form.serialize(),
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	            	App.unblockUI('#tenantAddDialogContent');
	                if(data.id){
	                	toastr["info"]("保存商户成功", "提示");
	                	$('#tenant_table').DataTable().draw(false);
	                	dialog.modal('hide');
	                }else{
	                	toastr["error"]("保存商户异常", "提示");
	                }
	                
	            }
	        });
		});
	}
	
	var initEditTenant = function(){
		$("#tenant_table").delegate('.edit','click',function(){
			
			var editId = $(this).data("editId");
			
			$("#tenantEditForm")[0].reset();
			var dialog = $("#tenantEditDialog");
			dialog.modal();
			App.blockUI({
                target: '#tenantEditDialogContent',
                boxed: true,
                message: '数据加载中...'
            });
			$.ajax({
	            url:context+'/app/tenant/load/'+editId,
	            type:'POST',
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	            	App.unblockUI('#tenantEditDialogContent');
	                if(data.success){
	                	iTsai.form.deserialize($('#tenantEditForm'),data.data)
	                }else{
	                	toastr["error"]("加载商户数据异常", "提示");
	                }
	                
	            }
	        });
			
		});
		
		
		$(".ok","#tenantEditDialog").on('click',function(){
			var dialog = $("#tenantEditDialog");
			var form = $("#tenantEditForm",dialog);
			
			App.blockUI({
                target: '#tenantEditDialogContent',
                boxed: true,
                message: '数据处理中...',
                textOnly: true
            });
			
			$.ajax({
	            url:context+'/app/tenant/save',
	            type:'POST',
	            data:form.serialize(),
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	            	App.unblockUI('#tenantEditDialogContent');
	                if(data.id){
	                	toastr["info"]("保存商户成功", "提示");
	                	$('#tenant_table').DataTable().draw(false);
	                	dialog.modal('hide');
	                }else{
	                	toastr["error"]("保存商户异常", "提示");
	                }
	                
	            }
	        });
		});
	}
	
	var initRemoveTenant = function(){
		$("#tenant_table").delegate('.remove','click',function(){
			
			var removeId = $(this).data("removeId");
			
			$.ajax({
	            url:context+'/app/tenant/delete/'+removeId,
	            type:'POST',
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	                if(data.success){
	                	toastr["info"]("删除商户成功", "提示");
	                	$('#tenant_table').DataTable().draw(false);
	                }else{
	                	toastr["error"]("删除商户异常", "提示");
	                }
	                
	            }
	        });
			
		});
	}
	
	var initQueryBtn = function(){
		$(".query").on('click',function(){
			var tableId = $(this).data("table");
			var table = $(tableId).DataTable();
			if(table){
				table.draw(false);
			}
		});
	}

	initTenantTable();
	initAddTenant();
	initEditTenant();
	initRemoveTenant();
	initQueryBtn();
	
});